package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author LEO
 * @Date 2021年12月16日 14:11
 */
@Controller

public class ListController {

    @Autowired
    private ListFeignClient listFeignClient;

    // http://list.gmall.com/list.html?category3Id=61
    // http://list.gmall.com/list.html?category3Id=61&trademark=5:小米
    @RequestMapping("list.html")
    public String listPage(SearchParam searchParam, HttpServletRequest request, Model model){
        // 远程调用如果只传递一个参数，那么在feign上 可以直接使用这个参数去接收
        // 远程调用如果传递多个参数 ，Long category3Id String trademark  不能，只能使用Json
        // searchParam 对象
        try {
            Result<Map> result= listFeignClient.list(searchParam);
            // SearchResponse s = result.getData();
//            request.setAttribute("goodsList","");
//            request.setAttribute("propsParamList","");
            // ${urlParam} 后台应该存储一个变量
            String urlParam=makeUrlParam(searchParam);
            model.addAttribute("urlParam",urlParam);
            model.addAttribute("searchParam",searchParam);

            // 品牌面包屑 trademarkParam 品牌：品牌名  可以看做一个字符串！品牌：小米
            String trademarkParam=makeTrademarkParam(searchParam.getTrademark());
            model.addAttribute("trademarkParam",trademarkParam);

            //${propsParamList} 平台属性面包屑  多个平台属性面包屑  集合
            List<Map> propsParamList=makeProps(searchParam.getProps());
            model.addAttribute("propsParamList",propsParamList);

            //orderMap 存储
            Map<String,Object> orderMap=this.dealOrder(searchParam.getOrder());
            model.addAttribute("orderMap",orderMap);


            model.addAllAttributes(result.getData());

        } catch (IOException e) {
            e.printStackTrace();
        }
        return "list/index";
    }

    // 获取用户排序规则
    //order=1:asc  order=1:desc
    private Map<String, Object> dealOrder(String order) {
        Map<String, Object> map = new HashMap<>();
        if (!StringUtils.isEmpty(order)){
            String[] split = order.split(":");
            if (split !=null && split.length==2){
                map.put("type",split[0]);
                map.put("sort",split[1]);
                return map;
            }
        }
        map.put("type",1);
        map.put("sort","asc");
        return map;
    }

    /**
     * 获取平台属性面包屑   &props=2:6.25-6.34英寸:屏幕尺寸&props=4:64G:机身存储
     * @param props
     * @return
     */
    private List<Map> makeProps(String[] props) {
        List<Map> list=new ArrayList<>();
        if (props!=null && props.length>0){
            for (String prop : props) {
               // prop =2:6.25-6.34英寸:屏幕尺寸
                String[] split = prop.split(":");
                if (split !=null && split.length==3){
                    //跟页面需求 创建一个map
                    HashMap<String,String> map = new HashMap<>();
                    map.put("attrId",split[0]);
                    map.put("attrValue",split[1]);
                    map.put("attrName",split[2]);
                    // 将用户点击的平台属性，属性值添加到map中
                    list.add(map);
                }
            }
        }
        return list;
    }

    /**
     * 获取品牌面包屑
     * @param trademark
     * @return
     */
    private String makeTrademarkParam(String trademark) {
        if (!StringUtils.isEmpty(trademark)){
            // trademark=2:华为 最终获取到品牌名称
            String[] split = trademark.split(":");
            if (split != null && split.length>0){
                return "品牌:"+split[1];
            }
        }
        return null;
    }

    /**
     * 获取请求路径中的请求参数
     * @param searchParam
     * @return
     */
    private String makeUrlParam(SearchParam searchParam) {
        StringBuilder stringBuilder = new StringBuilder();
        //http://list.gmall.com/list.html?keyword=手机
        // 判断用户是否通过关键字检索
        if (!StringUtils.isEmpty(searchParam.getKeyword())){
            stringBuilder.append("keyword=").append(searchParam.getKeyword());
        }

        //http://list.gmall.com/list.html?category3Id=61
        // 判断用户是否通过三级分类ID检索
        if (!StringUtils.isEmpty(searchParam.getCategory3Id())){
            stringBuilder.append("category3Id=").append(searchParam.getCategory3Id());
        }
        //http://list.gmall.com/list.html?category2Id=13
        // 判断用户是否通过二级分类ID检索
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())){
            stringBuilder.append("category2Id=").append(searchParam.getCategory2Id());
        }
        //http://list.gmall.com/list.html?category1Id=2
        // 判断用户是否通过一级分类ID检索
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())){
            stringBuilder.append("category1Id=").append(searchParam.getCategory1Id());
        }
        //http://list.gmall.com/list.html?category3Id=61&tradmark=4:小米
        // 判断用户是否通过品牌检索
        String trademark = searchParam.getTrademark();
        if (!StringUtils.isEmpty(trademark)){
            // 进行拼接
            if (stringBuilder.length()>0){
               stringBuilder.append("&trademark=").append(trademark);
            }
        }
        //http://list.gmall.com/list.html?category3Id=61&tradmark=4:小米&props=2342:4.7英寸:屏幕&props=2345:1:1
        // 判断用户是否通过平台属性检索
        String[] props = searchParam.getProps();
        if (props!=null && props.length>0){
            for (String prop : props) {
                if (stringBuilder.length()>0){
                    stringBuilder.append("&props=").append(prop);
                }
            }
        }

        // list.html 后面的参数列表
        return "list.html?"+stringBuilder.toString();
    }
}
